package com.content.Service.Impl.artist;

import com.common.Utils.JwtUtil;
import com.content.Entity.artist.Category;
import com.content.Mapper.artist.CategoryMapper;
import com.content.Service.artist.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageInfo<Category> getCategoryList(Integer pageNum, Integer pageSize, String categoriesName) {
        PageHelper.startPage(pageNum,pageSize);
        List<Category> categoryList = categoryMapper.getCategoryList(categoriesName);
        return new PageInfo<>(categoryList);

    }

    @Override
    public void addCategory(Category category) {
        String token = category.getToken();
        String username = JwtUtil.extractUsername(token);

        category.setCategoriesId(makeCategoryId());
        category.setCreateUser(username);
        category.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));

        categoryMapper.addCategory(category);
    }

    @Override
    public void updateCategory(Category category) {

        String token = category.getToken();
        String username = JwtUtil.extractUsername(token);
        category.setUpdateUser(username);
        category.setUpdateTime( LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        categoryMapper.updateCategory(category);
    }

    @Override
    public void deleteCategory(String categoriesId) {

        categoryMapper.deleteCategory(categoriesId);
    }

    @Override
    public Category getCategoryById(String categoryId) {
        return categoryMapper.getCategoryById(categoryId);
    }

    public String makeCategoryId(){
        String categoryId = "C" + System.currentTimeMillis();
        return categoryId;
    }
}
